package com.beaven.imclient.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.adapter.CategoryAdapter;
import com.beaven.imclient.base.BaseHideActivity;
import com.beaven.imclient.data.CategorySelect;
import com.beaven.imclient.presenter.user.CategoriesPresenter;
import com.beaven.imclient.presenter.user.CategoryContract;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.imclient.widget.CategoryDialogFragment;
import com.beaven.imclient.widget.DividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.List;

/**
 * 分组选择
 */
public class CategoryActivity extends BaseHideActivity implements CategoryContract.View {

  public static final String CATEGORY_RESULT = "CATEGORY_RESULT";

  public static void startForResult(Fragment fragment, int code) {
    Intent intent = new Intent(fragment.getActivity(), CategoryActivity.class);
    fragment.startActivityForResult(intent, code);
  }

  private CategoryContract.Presenter presenter;
  private CategoryAdapter categoryAdapter;

  private RecyclerView recycler;
  private CategorySelect categorySelected;
  private int selectedPos;

  @Override public int getLayoutId() {
    return R.layout.activity_category;
  }

  @Override protected void initWidget() {
    super.initWidget();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
    View view_status_bar = findViewById(R.id.view_status_bar);
    view_status_bar.setBackgroundColor(getResources().getColor(R.color.primary));
    TextView text_content_center = (TextView) findViewById(R.id.text_content_center);
    text_content_center.setText(R.string.category_manager);
    TextView text_arrow_right = (TextView) findViewById(R.id.text_arrow_right);
    text_arrow_right.setText(getString(R.string.sure));
    text_arrow_right.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (categorySelected != null) {
          // 返回选定分组ID
          Intent returnIntent = new Intent();
          returnIntent.putExtra(CATEGORY_RESULT, categorySelected.getCategory().getId());
          setResult(Activity.RESULT_OK, returnIntent);
          finish();
        }
      }
    });

    recycler = (RecyclerView) findViewById(R.id.category_recycler);
    categoryAdapter = new CategoryAdapter(R.layout.item_category_manager);
    recycler.setLayoutManager(new LinearLayoutManager(this));
    recycler.addItemDecoration(
        new DividerDecoration(CategoryActivity.this, DividerDecoration.VERTICAL_LIST));
    View footView = getFooterView(new View.OnClickListener() {
      @Override public void onClick(View v) {
        CategoryDialogFragment.newInstance().show(getSupportFragmentManager(), "");
      }
    });
    categoryAdapter.addFooterView(footView);
    recycler.setAdapter(categoryAdapter);

    categoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        setCategorySelect(position);
      }
    });
  }

  /**
   * 分组选中状态更新
   *
   * @param position 选中count
   */
  private void setCategorySelect(int position) {
    CategorySelect categorySelect = categoryAdapter.getItem(position);
    if (categorySelect != null) {
      categorySelect.setSelected(
          !categorySelect.isSelected() || categorySelect != categorySelected);
      categoryAdapter.setData(position, categorySelect);
      categoryAdapter.notifyItemChanged(position);
      if (categorySelected != null && categorySelect != categorySelected) {
        categorySelected.setSelected(false);
        categoryAdapter.setData(selectedPos, categorySelected);
        categoryAdapter.notifyItemChanged(selectedPos);
      }
      if (categorySelect.isSelected()) {
        categorySelected = categorySelect;
        selectedPos = position;
      } else {
        categorySelected = null;
        selectedPos = -1;
      }
    }
  }

  // 新建分组选项
  private View getFooterView(View.OnClickListener listener) {
    View view =
        getLayoutInflater().inflate(R.layout.item_category_add, (ViewGroup) recycler.getParent(),
            false);
    view.setOnClickListener(listener);
    return view;
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new CategoriesPresenter(this));
  }

  @Override protected void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override protected void onPause() {
    super.onPause();
    presenter.unSubscribe();
  }

  @Override public void setPresenter(CategoryContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void showList(final List<CategorySelect> categoryList) {
    runOnUiThread(new Runnable() {
      @Override public void run() {
        if (categorySelected == null) {
          categoryAdapter.setNewData(categoryList);
          setCategorySelect(0);
        } else {
          categoryAdapter.addData(categoryList);
        }
      }
    });
  }

  @Override public void showLoading() {

  }

  @Override public void hideLoading() {

  }

  @Override public void showText(int stringId) {
    ToastUtil.show(stringId);
  }

  public void create(String name) {
    presenter.addCategory(name);
  }
}
